package com.yj.contract.controller;

import com.yj.common.enums.OAUserRoleLevel;
import com.yj.common.responses.TerminalResponse;
import com.yj.config.mybatis.pagination.Page;
import com.yj.contract.dto.ContractChangeStatusDto;
import com.yj.contract.dto.ContractListDto;
import com.yj.contract.service.ContractService;
import com.yj.oa.entity.system.OAContract;
import com.yj.oa.entity.system.OAUserInfo;
import com.yj.oa.service.OAInfoService;
import com.yj.transport.service.TransportInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/8
 * Author     : Unbelievable9
 **/
@Api(tags = "易煤可视化-合同信息")
@Controller
@RequestMapping(value = "/contract")
public class ContractController {

    @Autowired
    ContractService contractService;

    @Autowired
    OAInfoService oaInfoService;

    @Autowired
    TransportInfoService transportInfoService;

    @ApiOperation("获取合同列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public TerminalResponse<Object> list(@RequestHeader("token") String token,
                                         @Validated ContractListDto contractListDto) {
        TerminalResponse<Object> terminalResponse;

        try {
            OAUserInfo oaUserInfo = oaInfoService.getOAUserByToken(token);

            if (oaUserInfo != null) {
                if (oaUserInfo.getRole_level().equals(OAUserRoleLevel.OPERATOR)) {
                    terminalResponse = new TerminalResponse<>(false, "用户为运营人员，请求接口错误", null);
                } else {
                    Page<OAContract> oaContractPage = contractService.getContractList(contractListDto, oaUserInfo);

                    terminalResponse = new TerminalResponse<>(true, "获取合同列表成功", oaContractPage);
                }
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户信息不存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "获取合同列表失败", null);
        }

        return terminalResponse;
    }

    @ApiOperation("修改合同状态")
    @PostMapping(value = "/change-status")
    @ResponseBody
    public TerminalResponse<Object> contractCancel(@RequestHeader("token") String token,
                                                   @RequestBody @Validated ContractChangeStatusDto contractChangeStatusDto) {
        TerminalResponse<Object> terminalResponse;

        try {
            OAUserInfo oaUserInfo = oaInfoService.getOAUserByToken(token);

            if (oaUserInfo != null) {
                contractService.changStatus(contractChangeStatusDto);

                terminalResponse = new TerminalResponse<>(true, "状态修改成功", null);
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户信息不存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "状态修改失败", null);
        }

        return terminalResponse;
    }

    @ApiOperation("获取合同详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public TerminalResponse<Object> detail(@RequestHeader("token") String token,
                                           @ApiParam(value = "合同main_id", required = true) @RequestParam String main_id) {
        TerminalResponse<Object> terminalResponse;

        try {
            OAUserInfo oaUserInfo = oaInfoService.getOAUserByToken(token);

            if (oaUserInfo != null) {
                OAContract oaContract = contractService.getContractByMainID(main_id);

                terminalResponse = new TerminalResponse<>(true, "获取合同详情成功", oaContract);
            } else {
                terminalResponse = new TerminalResponse<>(false, "用户信息不存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();

            terminalResponse = new TerminalResponse<>(false, "获取合同详情失败", null);
        }

        return terminalResponse;
    }
}
